package happywing.client.widget;

import java.util.ArrayList;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.layout.client.Layout;
import com.google.gwt.layout.client.Layout.Layer;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class Location {
	private ArrayList<Board> boardList=new ArrayList<Board>();
	private LayoutPanel body;
	private Board home;
	
	private boolean animateLock=false;
	
	public Location(LayoutPanel panel){
		this.body=panel;
		home=new Board();
		home.add(new Home());
		body.add(home);
	}
	
	public void goHome(){
		if(animateLock) return;
		animateLock=true;
		body.setWidgetTopHeight(home, -getHeight(), Unit.PX, getHeight(), Unit.PX);
		
		class Complete implements Layout.AnimationCallback {
			@Override
			public void onAnimationComplete() {
				boardList.clear();
				animateLock=false;
			}

			@Override
			public void onLayout(Layer layer, double progress) {
				
			}
		}
		body.forceLayout();
		body.setWidgetTopHeight(getCurrentBoard(), getHeight(), Unit.PX, getHeight(), Unit.PX);
		body.setWidgetTopBottom(home, 0, Unit.PX, 0, Unit.PX);
		Layout.AnimationCallback complete=new Complete();
		body.animate(500,complete);
	}
	
	public void go(Widget widget){
		if(animateLock) return;
		animateLock=true;
		Board board=new Board();
		board.add(widget);
		body.add(board);
		body.setWidgetTopHeight(board, -getHeight(), Unit.PX, getHeight(), Unit.PX);
		
		class Complete implements Layout.AnimationCallback {
			private Board board;
			
			Complete(Board board){
				this.board=board;
			}
			
			@Override
			public void onAnimationComplete() {
				boardList.clear();
				boardList.add(board);
				animateLock=false;
			}

			@Override
			public void onLayout(Layer layer, double progress) {
				
			}
		}
		body.forceLayout();
		body.setWidgetTopHeight(getCurrentBoard(), getHeight(), Unit.PX, getHeight(), Unit.PX);
		body.setWidgetTopBottom(board, 0, Unit.PX, 0, Unit.PX);
		Layout.AnimationCallback complete=new Complete(board);
		body.animate(500,complete);
		
		
	}
	
	public void next(Widget widget){
		if(animateLock) return;
		animateLock=true;
		Board board=new Board();
		board.add(widget);
		body.add(board);
		body.setWidgetLeftWidth(board, getWidth(), Unit.PX, getWidth(), Unit.PX);
		
		class Complete implements Layout.AnimationCallback {
			private Board board;
			
			Complete(Board board){
				this.board=board;
			}
			
			@Override
			public void onAnimationComplete() {
				boardList.add(board);
				animateLock=false;
			}

			@Override
			public void onLayout(Layer layer, double progress) {
				
			}
		}
		body.forceLayout();
		body.setWidgetLeftWidth(this.getCurrentBoard(), -getWidth(), Unit.PX, getWidth(), Unit.PX);
		body.setWidgetLeftRight(board, 0, Unit.PX, 0, Unit.PX);
		Layout.AnimationCallback complete=new Complete(board);
		body.animate(500,complete);
	}
	
	public void back(){
		if(animateLock) return;
		animateLock=true;
		Board board=(Board)boardList.get(boardList.size()-2);
		
		class Complete implements Layout.AnimationCallback {
			private Board board;
			
			Complete(Board board){
				this.board=board;
			}
			
			@Override
			public void onAnimationComplete() {
				boardList.remove(boardList.size()-1);
				animateLock=false;
			}

			@Override
			public void onLayout(Layer layer, double progress) {
				
			}
		}
		body.forceLayout();
		body.setWidgetLeftWidth(getCurrentBoard(), getWidth(), Unit.PX, getWidth(), Unit.PX);
		body.setWidgetLeftRight(board, 0, Unit.PX, 0, Unit.PX);
		Layout.AnimationCallback complete=new Complete(board);
		body.animate(500,complete);
	}
	
	private double getHeight(){
		return this.body.getOffsetHeight();
	}
	private double getWidth(){
		return this.body.getOffsetWidth();
	}
	
	private Board getCurrentBoard(){
		if(boardList.size()==0){
			return home;
		}else{
			return boardList.get(boardList.size()-1);
		}
	}

}
